package com.leyou.cart.service;

import com.leyou.auth.utils.UserInfo;
import com.leyou.cart.interceptors.LoginInterceptor;
import com.leyou.cart.pojo.Cart;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {


    @Autowired
    private StringRedisTemplate redisTemplate;
    static final String KEY_PREFIX = "ly:cart:uid:";

    public void addCart(Cart cart) {
        // 获取登录用户
        UserInfo user = LoginInterceptor.getLoginUser();
        // Redis的key
        String key = KEY_PREFIX + user.getId();
        // 获取hash操作对象
        BoundHashOperations<String, Object, Object> ops = this.redisTemplate.boundHashOps(key);
        // 查询是否存在
        String skuId = cart.getSkuId().toString();
        if (ops.hasKey(skuId)) {//已经存在该商品
            //从redis中获取该商品json字符串
            String jsonStr = ops.get(skuId).toString();
            Cart parse = JsonUtils.parse(jsonStr, Cart.class);
            cart.setNum(parse.getNum() + cart.getNum()); //已存在则商品数量相加

        }
        // 不存在，新增购物车数据
        ops.put(skuId, JsonUtils.serialize(cart));

    }

    public List<Cart> queryCartList() {
        UserInfo loginUser = LoginInterceptor.getLoginUser();

        String key = KEY_PREFIX + loginUser.getId();
        if (!redisTemplate.hasKey(key)) {
            return null;
        }
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(key);
        List<Cart> carts = new ArrayList<>();
        for (Object o : ops.values()) {
            Cart cart = JsonUtils.parse(o.toString(), Cart.class);
            carts.add(cart);
        }
        return carts;
    }

    //更新购物车数量
    public void updateNum(Long skuId, Integer num) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();
        Long userId = loginUser.getId();
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + userId);
        // 获取购物车
        String json = ops.get(skuId.toString()).toString();
        Cart cart = JsonUtils.parse(json, Cart.class);
        cart.setNum(num);
        ops.put(skuId.toString(), JsonUtils.serialize(cart));
    }

    public void deleteCart(String skuId) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getId());
        ops.delete(skuId);

    }

    public List<Cart> mergeCarts(List<Cart> cartList) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();
        //获取redis购物车
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getId());
        for (Cart cart : cartList) {
            //处理本地购物车和服务端购物车相同的商品合并
            Cart sCart = JsonUtils.parse(ops.get(cart.getSkuId().toString()).toString(), Cart.class);
            //System.out.println(sCart);
            if (sCart == null) {//远程购物车没有该商品
                ops.put(cart.getSkuId().toString(), JsonUtils.serialize(cart)); //放进远程购物车,有的话不作处理以远程中为主
                //其实也可以把远程和本地相同商品取商品数量高的更新到远程购物车
            }
        }
        //返回购物车数据到页面
        List<Cart> carts = new ArrayList<>();
        for (Object o : ops.values()) {
            Cart cart = JsonUtils.parse(o.toString(), Cart.class);
            carts.add(cart);
        }
        return carts;
    }
}
